<template>
  <div class="my_div">
    <el-dialog
      :title=" '新增'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
               label-width="80px">
        <el-form-item label="部门" prop="departmentId">
          <template slot-scope="scope">
            <el-select v-model="dataForm.hospitalId" filterable v-bind:disabled="dataForm.isHospitalManager==1"
                       placeholder="请选择医院" clearable @change="hospitalChanged">
              <el-option
                v-for="item in hospitalList"
                :key="item.hospitalId"
                :label="item.hospitalName"
                :value="item.hospitalId">
              </el-option>
            </el-select>
            <el-select v-model="dataForm.departmentId" filterable placeholder="请选择科室" clearable>
              <el-option
                v-for="item in departmentList"
                :key="item.departmentId"
                :label="item.departmentName"
                :value="item.departmentId">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="姓名" prop="patientName">
          <el-input v-model="dataForm.patientName" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idNumber">
          <el-input v-model="dataForm.idNumber" placeholder="身份证号码"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <template slot-scope="scope">
            <el-radio v-model="dataForm.gender" :label=1>男</el-radio>
            <el-radio v-model="dataForm.gender" :label=2>女</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="住院时间" prop="hospitalizationDate">
          <el-date-picker
            v-model="dataForm.hospitalizationDate"
            type="date"
            placeholder="选择住院时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入院照片" prop="hospitalizationPicture">
          <img :src="dataForm.patientImg" width="40" height="40" class="head_pic"/>
        </el-form-item>
        <el-form-item label="病床号" prop="wardNumber">
          <el-input v-model="dataForm.wardNumber" placeholder="病床号"></el-input>
        </el-form-item>
        <el-form-item label="病室号" prop="bedNumber">
          <el-input v-model="dataForm.bedNumber" placeholder="病室号"></el-input>
        </el-form-item>
        <el-form-item label="其他信息" prop="otherMessage">
          <el-input v-model="dataForm.otherMessage" placeholder="其他信息"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                hospitalList: '',
                departmentList: '',
                dataForm: {
                    departmentPatientId: 0,
                    //这个好
                    departmentId: '',
                    patientName: '',
                    idNumber: '',
                    gender: '',
                    wardNumber: '',
                    bedNumber: '',
                    hospitalizationDate: '',
                    leaveHospitalDate: '',
                    patientImg: '',
                    otherMessage: '',
                    remark: '',
                    hospitalId: this.$cookie.get('hospitalId') != "null" ? parseInt(this.$cookie.get('hospitalId')) : '',
                    isHospitalManager: this.$cookie.get('isHospitalManager')
                },
                dataRule: {
                    departmentId: [
                        {required: true, message: '部门id不能为空', trigger: 'blur'}
                    ],
                    patientName: [
                        {required: true, message: '姓名不能为空', trigger: 'blur'}
                    ],
                    idNumber: [
                        {required: true, message: '身份证号', trigger: 'blur'}
                    ],
                    gender: [
                        {required: true, message: '性别不能为空', trigger: 'blur'}
                    ],
                    hospitalizationDate: [
                        {required: true, message: '住院时间不能为空', trigger: 'blur'}
                    ],
                    patientImg: [
                        {required: true, message: '入院照片不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            //加载医院
            this.$http({
                url: this.$http.adornUrl(`/bs/hospital/getList`),
                method: 'get',
                params:this.$http.adornParams()
            }).then(({data}) => {
                if (data && data.code === 0) {
                    this.hospitalList = data.res;
                }
            })
            // 初始化查询，默认为前一天
            this.dataForm.hospitalizationDate = this.getDate();
        },
        methods: {
            hospitalChanged(hospitalId) {
                console.log(hospitalId)
                if (hospitalId != '') {
                    //加载科室
                    this.$http({
                        url: this.$http.adornUrl(`/bs/department/getListByHospitalId`),
                        method: 'get',
                        params: this.$http.adornParams({
                            hospitalId: hospitalId
                        })
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.dataForm.departmentId = '';
                            this.departmentList = data.res;
                        }
                    })
                } else {
                    this.departmentList = [];
                    this.dataForm.departmentId = '';
                }
            },
            getDate() {
                var myDate = new Date(); //获取系统当前时间
                return this.dateFtt("yyyy-MM-dd", myDate);
            },
            /**************************************时间格式化处理************************************/
            dateFtt(fmt, date) { //author: meizz
                var o = {
                    "M+": date.getMonth() + 1, //月份
                    "d+": date.getDate(), //日
                    "h+": date.getHours(), //小时
                    "m+": date.getMinutes(), //分
                    "s+": date.getSeconds(), //秒
                    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                    "S": date.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt))
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt))
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            },
            init(patientName, idNumber, gender, patientImg) {
                this.visible = true;
                this.dataForm.patientName = patientName;
                this.dataForm.idNumber = idNumber;
                if (gender.indexOf("男") > -1) {
                    this.dataForm.gender = 1;
                } else {
                    this.dataForm.gender = 2;
                }
                this.dataForm.patientImg = patientImg;
                //加载科室
                this.hospitalChanged(this.dataForm.hospitalId);
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/bs/departmentPatient/save`),
                            method: 'post',
                            data: this.$http.adornData({
                                'departmentId': this.dataForm.departmentId,
                                'hospitalizationDate': this.dataForm.hospitalizationDate,
                                'otherMessage': this.dataForm.otherMessage,
                                'remark': this.dataForm.remark,
                                'patientName': this.dataForm.patientName,
                                'idNumber': this.dataForm.idNumber,
                                'gender': this.dataForm.gender,
                                'patientImg': this.dataForm.patientImg,
                                'wardNumber': this.dataForm.wardNumber,
                                'bedNumber': this.dataForm.bedNumber,
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        // this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
<style>
  .my_div .el-dialog {
    float: right;
    margin-right: 30px;
  }
</style>
